package org.hzero.interfaces.common.helper.director;


import org.hzero.interfaces.common.helper.KnifeInterfaceLogHelper;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 创建日志,并进行持久化的中介者
 *
 * @author: tianhao.luo@hand-china.com 2021/7/1  下午12:07
 */
public class KnifeInterfaceLogDirector {
    private final KnifeInterfaceLogHelper knifeInterfaceLogHelper;

    public KnifeInterfaceLogDirector(KnifeInterfaceLogHelper knifeInterfaceLogHelper) {
        this.knifeInterfaceLogHelper = knifeInterfaceLogHelper;
    }

    /**
     * 创建日志并持久化
     *
     * @param args           监控方法的参数
     * @param responseEntity 监控调用返回的结果
     * @param method         反射拦截的方法
     */
    public void createHitfInterfaceLog(Object[] args, Object responseEntity, Method method) throws Exception{
        knifeInterfaceLogHelper.preDeal(method);
        knifeInterfaceLogHelper.buildInterfaceLogRequest(args,ServletRequest.class);
        knifeInterfaceLogHelper.buildInterfaceLogResponse(responseEntity, ServletResponse.class);
        knifeInterfaceLogHelper.enduranceHitfInterfaceLog();
    }


    /**
     * 创建日志并持久化，用于post入站请求，能够记录requestBody的路径参数
     *
     * @param map           监控方法的参数
     * @param responseEntity 监控调用返回的结果
     * @param method         反射拦截的方法
     */
    public void createHitfInterfaceLog(Map<String,Object> map, Object responseEntity, Method method) throws Exception{
        knifeInterfaceLogHelper.preDeal(method);
        knifeInterfaceLogHelper.buildInterfaceLogRequest(map,ServletRequest.class);
        knifeInterfaceLogHelper.buildInterfaceLogResponse(responseEntity, ServletResponse.class);
        knifeInterfaceLogHelper.enduranceHitfInterfaceLog();
    }
}
